home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / FlyThrough 1.1.2 / src / Source / Vehicle Classes / CFlyThroughDoc.cp next >
Encoding:
Text File  |  1997-03-17  |  4.1 KB  |  185 lines  |  [TEXT/CWIE]

  1. //
  2. //    CFlyThroughDoc.cp
  3. //
  4. //    A document class for opening a 3DMF file for viewing.
  5. //
  6. //    by James Jennings
  7. //    Started February 28, 1997
  8. //
  9.  
  10. #include "CFlyThroughDoc.h"
  11. #include "C3DMFReader.h"
  12. #include "Q3AutoObject.h"
  13.  
  14. #ifdef HAS_NEW_SCENE_MENU
  15. #include "CTriGridBox.h"
  16. #include "CBoxArrayMaker.h"
  17. #include "CScatteredBoxMaker.h"
  18. #endif // HAS_NEW_SCENE_MENU
  19.  
  20. const OSType    fileType_Creator    = 'Fly3';
  21. const OSType    fileType_3DMetafile = '3DMF';
  22.  
  23. const ResIDT    window_VehicleView    = 1001;
  24. const PaneIDT    pane_VehicleView    = 'VehV';
  25. const PaneIDT    slider_Thrust        = 'Thru';
  26.  
  27. CFlyThroughDoc::CFlyThroughDoc(LCommander *inSuper, FSSpec *inFSSpec)
  28.     : LSingleDoc(inSuper)
  29. {
  30.     Assert_(inFSSpec != nil);
  31.     
  32.     CVehicleViewPane *thePane = MakeWindow();
  33.     mWindow->SetDescriptor(inFSSpec->name);
  34.     
  35.     // read and install the 3DMF file
  36.     
  37.     ::SetCursor( *::GetCursor(watchCursor) );
  38.     
  39.     C3DMFReader theModel(inFSSpec);
  40.     
  41.     thePane->SetModel(theModel.Get());
  42.     
  43.     // If there are no view hints, view from the front.
  44.     if (theModel.GetViewHints() == nil)
  45.         thePane->ProcessCommand(cmd_ViewFromFront);
  46.     else // assume that the view hints specify a camera
  47.         thePane->SetViewHints(theModel.GetViewHints());
  48.     
  49.     
  50.     thePane->Refresh();
  51.     mWindow->Show();
  52. }
  53.  
  54. #ifdef HAS_NEW_SCENE_MENU
  55. CFlyThroughDoc::CFlyThroughDoc(LCommander *inSuper, CommandT inCommand)
  56.     : LSingleDoc(inSuper)
  57. {
  58.     CVehicleViewPane *thePane = MakeWindow();
  59.     
  60.     // build the scene that corresponds to the command
  61.     switch ( inCommand ) {
  62.     case cmd_8Cubes:
  63.         {
  64.             CBoxArrayMaker maker(2);
  65.             thePane->SetModel( maker.Get() );
  66.         }
  67.         break;
  68.     case cmd_27Cubes:
  69.         {
  70.             CBoxArrayMaker maker(3);
  71.             thePane->SetModel( maker.Get() );
  72.         }
  73.         break;
  74.     case cmd_64Cubes:
  75.         {
  76.             CBoxArrayMaker maker(4);
  77.             thePane->SetModel( maker.Get() );
  78.         }
  79.         break;
  80.     case cmd_Scatter20Box:
  81.         {
  82.             CScatteredBoxMaker maker;
  83.             thePane->SetModel( maker.Get() );
  84.         }
  85.         break;
  86.     case cmd_Scatter40Box:
  87.         {
  88.             CScatteredBoxMaker maker(40);
  89.             thePane->SetModel( maker.Get() );
  90.         }
  91.         break;
  92.     case cmd_TriGridBox10:
  93.         {
  94.             CTriGridBox maker(4,10);
  95.             thePane->SetModel( maker.Get() );
  96.         }
  97.         break;
  98.     case cmd_TriGridBox30:
  99.         {
  100.             CTriGridBox maker(4,30);
  101.             thePane->SetModel( maker.Get() );
  102.         }
  103.         break;
  104.     default:
  105.         Assert_(false /* new document called with unknown command */);
  106.     }
  107.     
  108.     thePane->Refresh();
  109.     mWindow->Show();
  110.     
  111. }
  112. #endif // HAS_NEW_SCENE_MENU
  113.  
  114. CVehicleViewPane *    CFlyThroughDoc::MakeWindow()
  115. {
  116.     mWindow = LWindow::CreateWindow(window_VehicleView, this);
  117.     Assert_( mWindow != nil );
  118.     
  119.     // Arrange for the QuickDraw 3D view to receive commands.
  120.     CVehicleViewPane *thePane = (CVehicleViewPane*)
  121.                         mWindow->FindPaneByID( pane_VehicleView );
  122.     Assert_( thePane != nil );
  123.     
  124.     mWindow->SetLatentSub( thePane );
  125.     
  126.     // Connect the Thrust slider to the Vehicle
  127.     
  128.     LControl *theSlider = (LControl*) mWindow->FindPaneByID( slider_Thrust );
  129.     theSlider->AddListener( thePane->GetVehicle() );
  130.     
  131.     return thePane;
  132. }
  133.  
  134. void    CFlyThroughDoc::DoAESave( FSSpec &inFileSpec, OSType inFileType )
  135. {
  136.     // Create the new file.
  137.     LFile macFile( inFileSpec );
  138.     macFile.CreateNewFile( fileType_Creator, fileType_3DMetafile );
  139.     
  140.     // Create storage and file objects, and link them
  141.     Q3AutoObject<TQ3StorageObject> theStorage( ::Q3FSSpecStorage_New( &inFileSpec ) );
  142.     ThrowIfNil_(theStorage.Get());
  143.     
  144.     Q3AutoObject<TQ3FileObject> theFile(::Q3File_New());
  145.     ThrowIfNil_(theFile.Get());
  146.     
  147.     TQ3Status theStatus;
  148.     theStatus = ::Q3File_SetStorage(theFile, theStorage);
  149.     ThrowIfQ3Fail_(theStatus);
  150.     
  151.     // Open the file...
  152.     if ( ::Q3File_OpenWrite(theFile, 
  153.                 kQ3FileModeNormal /*| kQ3FileModeText*/) == kQ3Success ) {
  154.         
  155.         Assert_( mWindow != nil );
  156.         CVehicleViewPane *thePane = (CVehicleViewPane*)
  157.                             mWindow->FindPaneByID( pane_VehicleView );
  158.         Assert_( thePane != nil );
  159.         
  160.         // ... and write to it
  161.         thePane->Write(theFile);
  162.         
  163.         // To do: View Hints.
  164.         
  165.         theStatus = ::Q3File_Close( theFile );
  166.     //    ThrowIfQ3Fail_(theStatus);
  167.         
  168.     } else {
  169.         // post error alert
  170.         TQ3Error q3Err;
  171.         q3Err = ::Q3Error_Get( &q3Err );
  172.         switch (q3Err) {
  173.         case kQ3ErrorMacintoshError:
  174.             OSErr macErr;
  175.             ::Q3MacintoshError_Get( &macErr );
  176.             // ...
  177.             break;
  178.         case kQ3ErrorOutOfMemory:
  179.         default:    // unknown error
  180.             break;
  181.         }
  182.         
  183.     }
  184. }
  185.